package com.lonni.rpc.core.serialize;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * FastJson序列化
 * @author: Lonni
 * @date: 2024/6/27 0027 11:27
 */
public class FastJsonSerialize implements IRpcSerialize,Serializable {


    /**
     * 序列化
     *
     * @param t
     * @return
     */
    public <T> byte[] serialize(T t) throws UnsupportedEncodingException {
        String string = JSON.toJSONString(t);
        return  string.getBytes("UTF-8");
    }

    /**
     * 反序列化
     *
     * @param data
     * @param clazz
     * @return
     */
    public <T> T deserialize(byte[] data, Class<T> clazz) {
        return JSON.parseObject(new String(data),clazz);
    }
}



